package com.example.houselease.controller;

import com.example.houselease.pojo.dto.admin.AdminAddNewDTO;
import com.example.houselease.pojo.dto.admin.AdminLoginDTO;
import com.example.houselease.pojo.dto.admin.AdminUpdateDTO;
import com.example.houselease.pojo.vo.AdminDetailVO;
import com.example.houselease.pojo.vo.AdminListVO;
import com.example.houselease.pojo.vo.RoleDetailVO;
import com.example.houselease.service.IAdminService;
import com.example.houselease.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理员 controller
 * @author sf
 * @version 0.0.1
 * */
@RestController
@Slf4j
@Api(tags = "1. 管理员管理模块")
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 100)
    JsonResult<String> login(@Valid AdminLoginDTO adminLoginDTO){
        log.debug("开始处理【管理员登录】请求，参数：{}",adminLoginDTO);
        String jwt = iAdminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('house/admin/add-new')")
    JsonResult<Void> add(@Valid AdminAddNewDTO adminLoginDTO){
        log.debug("开始处理【添加管理员】请求，参数：{}",adminLoginDTO);
        iAdminService.addNew(adminLoginDTO);
        return JsonResult.ok();
    }

    @DeleteMapping("/{id:[0-9]+}")
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "数据id",value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/admin/delete')")
    JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【删除管理员】请求，参数：{}",id);
        iAdminService.deleteById(id);
        return JsonResult.ok();
    }

    @PutMapping("/{id:[0-9]+}")
    @ApiOperation("修改管理员")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "数据id",value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/admin/update')")
    JsonResult<Void> update(@PathVariable Long id, @Valid AdminUpdateDTO adminUpdateDTO){
        log.debug("开始处理【修改管理员】请求，参数1：{}，参数2：{}",id,adminUpdateDTO);
        iAdminService.update(id,adminUpdateDTO);
        return JsonResult.ok();
    }

    @PutMapping("/role/{id:[0-9]+}")
    @ApiOperation("修改管理员所拥有的角色")
    @ApiOperationSupport(order = 401)
    @ApiImplicitParam(name = "管理员数据id",value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/admin/update')")
    JsonResult<Void> updateRole(@PathVariable Long id, @RequestBody List<Long> roleIds){
        log.debug("开始处理【修改管理员所拥有的角色】请求，参数1：{}，参数2：{}",id,roleIds);
        iAdminService.updateAdminRoleByAdminId(id,roleIds);
        return JsonResult.ok();
    }

    @PutMapping("/enable/{id:[0-9]+}")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 402)
    @ApiImplicitParam(name = "管理员数据id",value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/admin/update')")
    JsonResult<Void> enable(@PathVariable Long id){
        log.debug("开始处理【启用管理员】请求，参数1：{}",id);
        iAdminService.enable(id);
        return JsonResult.ok();
    }

    @PutMapping("/disable/{id:[0-9]+}")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 403)
    @ApiImplicitParam(name = "管理员数据id",value = "id",required = true)
    @PreAuthorize("hasAnyAuthority('house/admin/update')")
    JsonResult<Void> disable(@PathVariable Long id){
        log.debug("开始处理【禁用管理员】请求，参数1：{}",id);
        iAdminService.disable(id);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAnyAuthority('house/admin/read')")
    JsonResult<List<AdminListVO>> list(){
        log.debug("开始处理【管理员列表】请求");
        return JsonResult.ok(iAdminService.list());
    }

    @GetMapping("/role/{id:[0-9]+}")
    @ApiOperation("管理员所拥有角色")
    @ApiOperationSupport(order = 501)
    @PreAuthorize("hasAnyAuthority('house/admin/read','house/role/read')")
    JsonResult<List<Long>> roleInfo(@PathVariable Long id){
        log.debug("开始处理【管理员所拥有角色数据】请求");
        return JsonResult.ok(iAdminService.getRoleByAdminId(id));
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("管理员详情")
    @ApiOperationSupport(order = 502)
    @PreAuthorize("hasAnyAuthority('house/admin/read')")
    JsonResult<AdminDetailVO> standard(@PathVariable Long id){
        log.debug("开始处理【管理员详情】请求");
        return JsonResult.ok(iAdminService.getAdminDetail(id));
    }
}
